/**
 * 
 */
package com.net.web.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.net.domain.Staff;
import com.net.domain.Student;
import com.net.domain.User;
import com.net.domain.UserType;
import com.net.service.UserService;
import com.net.service.UtilService;
import com.net.web.form.LoginForm;

/**
 * @author dinesh
 *
 */
public class UserActionController extends MultiActionController {
	
	
	final static Logger log = Logger.getLogger(UserActionController.class);
	
	//private StudentService studentService;
	private UserService userService;
	
	public ModelAndView login(HttpServletRequest req, HttpServletResponse res) {
		
		ModelAndView mav = new ModelAndView("home");
		
		try {
			
			LoginForm loginForm = new LoginForm();
			
			loginForm.setUserName(req.getParameter("user_name"));
			loginForm.setPassword(req.getParameter("password"));
			
			// Read the form data from HTTP request
			
			User user = userService.doSignIn(loginForm);
			
			
			if(user == null) {
				mav.setViewName("authenticationfailure");
				return mav;
			}
			
//			mav.setViewName("studenthome");
			//log.info(user);
			
			user = userService.populateUserDetails(user);
			
			if(user.getUserType() == UserType.STUDENT.getUserType())
				mav.setViewName("studenthome");
			else if(user.getUserType() == UserType.STAFF.getUserType())
				mav.setViewName("studenthome");
//				mav.setViewName("staffhome");
				
			//List<Student> list = studentService.getAllStudents();
			//log.debug("The number of student records " + list.size() );
			
			req.getSession(true).setAttribute("user", user);
			
			//mav.addObject("students", list);
			mav.addObject("user", user);
		}
		catch(Exception e) {
			log.error(" Error caught while logging the user " + e);
		}
		
		return mav;
	}
	
	
	
	public ModelAndView updateUserDetail(HttpServletRequest req, HttpServletResponse res) {
		
		ModelAndView mav = new ModelAndView("home");
		
		try {
			User user = (User) req.getSession(false).getAttribute("user");
			
			// 
			if(user == null) {
				mav.setViewName("authenticationfailure");
				return mav;
			}
			log.info("first" + req.getParameter("first"));
			// Check whether first time hit
			if(req.getParameter("first") != null && req.getParameter("first").equals("1")) {
				log.info("Inside update");
				// Now get the values from Request form data
				user.setFirstName((String) req.getParameter("first_name"));
				user.setLastName((String) req.getParameter("last_name"));
				user.setMiddleName((String) req.getParameter("middle_name"));
				user.setTitle((String) req.getParameter("title"));
				user.setGender((String) req.getParameter("gender"));
				log.debug(req.getParameter("gender"));
				user.setCommunicationAddress((String) req.getParameter("communication_address"));
				user.setPrimaryEmail((String) req.getParameter("primary_email"));
				user.setSecondaryEmail((String) req.getParameter("secondary_email"));
				user.setMobileNo((String) req.getParameter("mobile_no"));
				user.setLandLineNo((String) req.getParameter("land_line_no"));
				user.setDob((String) req.getParameter("dob"));
				
				if(user.getUserType() == UserType.STUDENT.getUserType()){
				Student student=(Student)user;
				student.setRegisterNo((String) req.getParameter("register_no"));
				student.setRollNo((String) req.getParameter("roll_no"));
				}
				if(user.getUserType() == UserType.STAFF.getUserType()){
					Staff staff=(Staff)user;
//					staff.setStaffId((String)req.getParameter("staff_id"));
				}
				// Now update to the database
				
				userService.updateUserDetails(user);
				
				mav.addObject("first", 1);
				mav.setViewName("studentupdate");
			}
			else {
				mav.addObject("first", 1);
				mav.setViewName("studentupdate");
			}
			
			mav.addObject("user", user);
		}
		catch (Exception e) {
			log.error("Error caught while running updateUserDetail", e);
		}
		
		return mav;
	}

	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param userService the userService to set
	 */
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
